﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Data.Common;
using System.Data.SQLite;
using System.Collections.ObjectModel;
using LinqToDB.Common;

namespace Cashier
{
    /// <summary>
    /// OderDetailPage.xaml 的交互逻辑
    /// </summary>
    public partial class OderDetailPage : Page
    {
        private double sum = 0;//总价
        public OderDetailPage()
        {
            InitializeComponent();
            
            ShowMenu();
        }
        public void clearTable()
        {
            try
            {
                SQLiteConnection conn = new SQLiteConnection(CommonValue.sqliteConnectString);
                conn.Open();
                String cmd = "select table_id from desktable where number_of_people>0";
                SQLiteCommand mycmd = new SQLiteCommand(cmd, conn);
                SQLiteDataReader reader = mycmd.ExecuteReader();
                while (reader.Read())
                {
                    int table_id = reader.GetInt32(0);
                    cmd = "select count(*) from oder where table_id=" + table_id + " and compelete=0";
                    mycmd = new SQLiteCommand(cmd, conn);
                    int value = (int)mycmd.ExecuteScalar();
                    if (value == 0)
                    {
                        // 当前餐桌没有订单
                        Console.WriteLine("没有订单");
                    }
                }
                //if (mycmd.ExecuteNonQuery() > 0)
                //{
                //    double change = Convert.ToDouble(paidBox.Text.ToString()) - sum;
                //    changeBox.Text = "支付成功！找零" + change;
                //    conn.Close();
                //}
            }
            catch (Exception e)
            {
                changeBox.Text = "对不起，支付失败，请再次尝试！" + e.Message;
            }
        }
        private void button_Click(object sender, RoutedEventArgs e)
        {
            Pay();
        }

        private void ShowMenu()
        {
            try
            {
               
                //获取表格
                DataTable data = new DataTable("food");
                //DataColumn food_id = new DataColumn("food_id");
                data.Columns.Add(new DataColumn("food_id", typeof(string)));
                data.Columns.Add(new DataColumn("name", typeof(string)));
                data.Columns.Add(new DataColumn("price", typeof(string)));
                data.Columns.Add(new DataColumn("number", typeof(string)));
                data.Columns.Add(new DataColumn("total_price", typeof(string)));
                data.Columns.Add(new DataColumn("table_id", typeof(int)));

                using (SQLiteConnection conn = new SQLiteConnection(CommonValue.sqliteConnectString))
                {
                    conn.Open();
                    string cmd = "select * from food_odered where oder_id='" + CommonValue.FOOD_PAY_ID + "'";
                    using (SQLiteCommand myCmd = new SQLiteCommand(cmd, conn))
                    {
                        using (SQLiteDataReader reader = myCmd.ExecuteReader())
                        {
                            ObservableCollection<Food> memberData = new ObservableCollection<Food>();
                            while (reader.Read())
                            {

                                string food_id = reader["food_id"].ToString();
                                string name = reader["food_name"].ToString();
                                string price = reader["price"].ToString();
                                string number = reader["number"].ToString();
                                string total_price = reader["total_price"].ToString();
                                sum += Convert.ToDouble(total_price);
                                memberData.Add(new Food()
                                {
                                    food_id = food_id,
                                    name = name,
                                    price = price,
                                    number = number,
                                    total_price = total_price
                                });

                            }
                            dataGrid.DataContext = memberData;
                            sumBlock.Text = sum.ToString();
                            conn.Close();
                        }
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("查询菜单失败:" + e.Message);
            }

        }
        private SQLiteConnection conn;
        public void Pay()
        {
            try
            {
                if (paidBox.Text.ToString() != "")
                {
                    double paid = Convert.ToDouble(paidBox.Text.ToString());
                    if (paid >= sum)
                    {
                        using (conn = new SQLiteConnection(CommonValue.sqliteConnectString))
                        {
                            conn.Open();
                            String cmd = "update oder set complete=1, paid ='" + paid + "',change='"+(paid-sum)+"'  where oder_id='" + CommonValue.FOOD_PAY_ID + "'";
                            using (SQLiteCommand mycmd = new SQLiteCommand(cmd, conn))
                            {
                                if (mycmd.ExecuteNonQuery() > 0)
                                {
                                    double change = Convert.ToDouble(paidBox.Text.ToString()) - sum;
                                    changeBox.Text = "支付成功！找零" + change;

                                }
                            }
                        }
                        updateSigleTable();
                    }
                    else
                    {
                        changeBox.Text = "支付金额不足，请确认金额足够，重新支付！";
                    }
                }
                else
                {
                    MessageBox.Show("输入金额为空！");
                }
            }
            catch (Exception e)
            {
                changeBox.Text = "对不起，支付失败，请再次尝试！" + e.Message;
            }

        }
        private bool updateTable(string table_id)
        {
            try
            {


                string cmd = "select count(*) from oder where complete=0 and table_id = " + table_id;
                bool flag = false;

                using (SQLiteCommand SQLiteCommand = new SQLiteCommand(cmd, conn))
                {
                    //SQLiteCommand.ExecuteScalar();
                    //Console.WriteLine(SQLiteCommand.ExecuteScalar());
                    Object value = SQLiteCommand.ExecuteScalar();
                    if (value != null && (Convert.ToInt32(value)) == 0)
                    {
                        flag = true;

                    }



                }


                if (flag == true)
                {
                    string updateCmd = "update desktable set number_of_people=0 where table_id=" + table_id;
                    using (SQLiteCommand command = new SQLiteCommand(updateCmd, conn))
                    {
                        command.ExecuteNonQuery();
                        return false;
                    }
                }

            }
            catch (Exception e)
            {
                MessageBox.Show("台桌查询失败：" + e.Message);
            }
            return true;
        }
        private void updateSigleTable()
        {
            using (conn = new SQLiteConnection(CommonValue.sqliteConnectString))
            {
                conn.Open();
                String cmd = "select table_id from oder where oder_id="+CommonValue.FOOD_PAY_ID;
                using (SQLiteCommand mycmd = new SQLiteCommand(cmd, conn))
                {
                    long value = Convert.ToInt64(mycmd.ExecuteScalar());
                    updateTable(value.ToString());


                }
            }
        }
        public class Food
        {
            public string food_id { get; set; }
            public string name { get; set; }
            public string price { get; set; }
            public string category { get; set; }
            public string number { get; set; }
            public string total_price { get; set; }

        }
    }
}
